﻿namespace ETestIT.UI
{
    using System;
    using System.Web.UI.WebControls;
    using Core;
    using Core.Infrastructure;
    using Core.Services;
    using Presentation;
    using Presentation.Storage;
    using SharpArch.Core.PersistenceSupport;

    public partial class Resumes : PageBase<Resumes>
    {
        ISessionProvider sessionStorage;
        IRepository<Resume> resumeRepository;
        ICandidateResumeService candidateResumeService;
        IRepository<Candidate> cadidateRepository;

        protected void Page_Load(object sender, EventArgs e)
        {
            sessionStorage = IoC.Resolve<ISessionProvider>();
            resumeRepository = IoC.Resolve<IRepository<Resume>>();
            cadidateRepository = IoC.Resolve<IRepository<Candidate>>();
            candidateResumeService = IoC.Resolve<ICandidateResumeService>();

            resumeAddUpdate.Update += ResumeAddUpdateUpdate;

            if(!IsPostBack)
            {
                if (sessionStorage.Contains(Const.SelectedCandidateKey))
                    LoadCandidateResumes();
            }
        }

        private void LoadCandidateResumes()
        {
            var candidateId = sessionStorage.Get<int>(Const.SelectedCandidateKey);

            gvReumes.DataSource = candidateResumeService.GetLatestVersionsResume(candidateId);
            gvReumes.DataBind();
        }

        protected void LnkResumesViewCommand(object sender, CommandEventArgs e)
        {
            var resumeId = int.Parse(e.CommandArgument.ToString());

            switch (e.CommandName)
            {
                case "ViewCandidateResume":
                    {
                        var resume = resumeRepository.Get(resumeId);
                        Response.WriteResumeToResponse(resume);
                        break;    
                    }
                case "UpdateCandidateResume" :
                    {
                        var resume = resumeRepository.Get(resumeId);
                        sessionStorage.Set(Const.SelectedResumeKey, resumeId);
                        resumeAddUpdate.SetModel(resume);
                        resumeAddUpdate.Visible = true;
                        break;
                    }
                case "ViewCandidateResumesHistory" :
                    {
                        sessionStorage.Set(Const.SelectedCandidateResumeKey, resumeId);
                        Response.Redirect("~/ResumeHistory.aspx");
                        break;
                    }
                case "RemoveCandidateResume" :
                    {
                        With.Transaction(() =>
                         {
                             candidateResumeService.Delete(resumeId);
                         });
                        break;
                    }    
            }

            LoadCandidateResumes();
        }

        protected void BtnAddNewResume_Clicked(object sender, EventArgs e)
        {
            resumeAddUpdate.Visible = true;
        }

        public void ResumeAddUpdateUpdate(object sender, EventArgs e)
        {
            var resume =  new Resume();
            var isUpdate = sessionStorage.Contains(Const.SelectedResumeKey);

            var candidateId = sessionStorage.Get<int>(Const.SelectedCandidateKey);
            var candidate = cadidateRepository.Get(candidateId);

            resumeAddUpdate.UpdateModel(resume);

            With.Transaction(()=>
             {
                 if (isUpdate)
                 {
                     var updatedResumeId = sessionStorage.Get<int>(Const.SelectedResumeKey);
                     candidateResumeService.Update(updatedResumeId, resume);
                     sessionStorage.Remove(Const.SelectedResumeKey);
                 }
                 else
                 {
                     var jobTitle = resumeAddUpdate.SelectedJobTitle;
                     candidateResumeService.Add(candidate, jobTitle, resume);
                 }

                 Dialog.Information = "The resumes list was updated successifully";
             });

            LoadCandidateResumes();
            resumeAddUpdate.Visible = false;
        }
    }
}